package com.asen.commons.utils;

import com.asen.commons.core.generator.IdGenerator;
import com.asen.commons.core.generator.factory.IdGeneratorFactory;

/**
 * ID处理工具类
 *
 * @author Gary
 * @version 1.0
 * @date 2018-11-19
 */
public class IdUtils {

    /**
     * 根据Snowflake算法生成ID
     *
     * @return
     */
    public static Long genIdBySnowflake() {
        IdGenerator idGenerator = IdGeneratorFactory.getSnowflakeIdGenerator();
        return idGenerator.nextId();
    }

    /**
     * 根据redis生成ID
     *
     * @param cls
     * @return
     */
    public static Long genIdByRedis(Class cls) {
        return IdUtils.genIdBySnowflake();
    }

    public static boolean isEmpty(Long id) {
        return id == null || id == 0L;
    }

    public static boolean isNotEmpty(Long id) {
        return !isEmpty(id);
    }

    public static boolean equal(Long id1, Long id2) {
        if (id1 == null || id2 == null) {
            return false;
        }

        return id1.equals(id2);
    }

    public static boolean notEqual(Long id1, Long id2) {
        return !equal(id1, id2);
    }

    /**
     * 多个值存在空值
     */
    public static boolean anyEmpty(Long... ids) {
        if (ids == null | ids.length == 0) {
            return true;
        }
        boolean isEmpty = false;
        for (Long id : ids) {
            isEmpty = isEmpty || IdUtils.isEmpty(id);
        }
        return isEmpty;
    }

    /**
     * 多个值全部为空
     */
    public static boolean allEmpty(Long... ids) {
        if (ids == null | ids.length == 0) {
            return true;
        }
        boolean isEmpty = true;
        for (Long id : ids) {
            isEmpty = isEmpty && IdUtils.isEmpty(id);
        }
        return isEmpty;
    }
}
